/***********************************************
 @Name :        小铭同学
 @Time :        2025-08-15
 @Email :       LiaoMingWJ233@163.com
 @Comment :     自定义控件 TCP 通讯
 @FileName :    tcp.cpp
 @Version :     1.0
************************************************/


#include "tcp.h"

TCP::TCP(QWidget *parent) : QWidget(parent)
{
    TcpSocket = new QTcpSocket(this);
    
    // 成功连接到服务器时触发
    connect(TcpSocket , &QTcpSocket::connected , this , [&](){
        qDebug() << "连接成功";
    });
    
    // 连接断开时触发
    connect(TcpSocket , &QTcpSocket::disconnected , this , [&](){
        qDebug() << "断开连接";
    });

    // 最重要的信号，当有新数据可读时触发
    //connect(TcpSocket, &QTcpSocket::readyRead, this, &TCP::readData);
}

void TCP::TCP_send(QString data)
{
    TcpSocket->write(data.toUtf8());
}

void TCP::TCP_connect()
{
    TcpSocket->connectToHost("192.168.10.148" , 8080);
    qDebug() << "启动连接";
}

void TCP::readData()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
    if (!socket)
        return;
    
    QString data = socket->readAll();        
    qDebug() <<data<<endl;    

    //ui->textEdit_js->setText(read);
    //ui->textEdit_2->setText(data);
}

